#include "Frm03.h"
#include "util/DateUtil.h"

Frm03::Frm03(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm03::~Frm03() {
}

void Frm03::onInit() {

}

void Frm03::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(1, 1, 98, 48, false);
	wMButton1->setPressedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(925, 35, 98, 48, false);
	wMButton2->setPressedArg("发布", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("发布", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm03::wMButton2_clk_cb);

	/*----------------------------- Label : wMLabel1                     -----------------------------*/
	wMLabel1 = new STTLabel(p_rt, this, "wMLabel1");
	wMLabel1->setMainArg(135, 19, 100, 31, "发布主题", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel1->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel1->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Text : wMText1                       -----------------------------*/
	wMText1 = new STTTextArea(p_rt, this, "wMText1");
	wMText1->setMainArg(241, 16, 664, 38, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText1->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 5, 6, 4, 4, 255);
	wMText1->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::UC);
	wMText1->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, this, "wMLabel2");
	wMLabel2->setMainArg(135, 65, 100, 31, "发布内容", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Text : wMText2                       -----------------------------*/
	wMText2 = new STTTextArea(p_rt, this, "wMText2");
	wMText2->setMainArg(241, 62, 664, 38, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText2->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 5, 6, 4, 4, 255);
	wMText2->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::UC);
	wMText2->setFont(0x0, 16, "文泉驿微米黑", 0);
	wMText2->setText("{\"data\":{\"temp_current\":12}}");

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(135, 116, 100, 31, "订阅主题", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Text : wMText3                       -----------------------------*/
	wMText3 = new STTTextArea(p_rt, this, "wMText3");
	wMText3->setMainArg(241, 113, 664, 38, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText3->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 5, 6, 4, 4, 255);
	wMText3->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::UC);
	wMText3->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel4                     -----------------------------*/
	wMLabel4 = new STTLabel(p_rt, this, "wMLabel4");
	wMLabel4->setMainArg(135, 162, 100, 31, "订阅内容", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel4->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel4->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel4->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextArea : wMTextArea1               -----------------------------*/
	wMTextArea1 = new STTTextArea(p_rt, this, "wMTextArea1");
	wMTextArea1->setMainArg(241, 162, 664, 340, false, false, TextAlign::LEFT, -1, -1, -99999999, 99999999, false);
	wMTextArea1->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 4, 4, 4, 4, 255);
	wMTextArea1->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::NA);
	wMTextArea1->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*called after widget created.*/

	/*----------------------------- 变量消息 : wMVar2                     -----------------------------*/
	_V("aws2")->valueChanged.connect(this, &Frm03::onwMVar2Changed);
	std::function<void()> diswMVar2 = [this]() {
		_V("aws2")->valueChanged.disconnect(this, &Frm03::onwMVar2Changed);
	};
	addVarMsg(diswMVar2);

	return; //createParts END
}

void Frm03::onLoad() {
	Dvchar dv1 = (Dvchar)vm->get("aws1");//获取设备变量 - 发布
	wMText1->setText(dv1->getAddress());

	wMText3->setEnable(false);
	Dvchar dv = (Dvchar)vm->get("aws2");//获取设备变量 - 订阅
	wMText3->setText(dv->getAddress());
}

bool Frm03::onClosing() {
	return true;
}

void Frm03::onDispose() {

}

void Frm03::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（发布）的点击事件*/
	Dvchar dv = (Dvchar)vm->get("aws1");//获取设备变量 - 发布
	dv->setAddress(wMText1->getText());//如需改变发布主题，加这句
	dv->set(wMText2->getText());//发布信息
}

void Frm03::onwMVar2Changed(SttObject *org, IVariable *data) {
	/*变量消息*/
	string v;
	vm->getChar("aws2", v);
	cout<<v<<endl;
	string w;
	vm->getChar("aws3", w);
	w = DateUtil::getTimeString() + "消息来自：" + w + "       ";
	cout<<w+v<<endl;
	wMTextArea1->addText(w);
	wMTextArea1->addText("\n");
	wMTextArea1->addText(v);
	wMTextArea1->addText("\n");
	wMTextArea1->addText("---------------------");
	wMTextArea1->addText("\n");
}
